/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.liquidByte.trafficMustang.server.webService;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import org.liquidByte.trafficMustang.server.dataModel.Localizacao;
import org.liquidByte.trafficMustang.server.dataModel.Usuario;
import org.liquidByte.trafficMustang.server.services.ConverterService;
import org.liquidByte.trafficMustang.server.services.RastreamentoService;
import org.liquidByte.trafficMustang.server.webService.base.WebService;
import org.liquidByte.trafficMustang.server.webService.dto.LocalizacaoDTO;
import org.liquidByte.trafficMustang.server.webService.dto.UsuarioDTO;

/**
 * REST Web Service
 *
 * @author Alexandre
 */
@Stateless
@Path("rastreamento")
public class RastreamentoWebService extends WebService {
    
    @EJB
    private RastreamentoService service;
    @EJB
    private ConverterService converter;
    
    @POST
    @Produces("application/json")
    @Consumes("application/json")
    @Path("update")
    public String update(String data) {
        try {
            LocalizacaoDTO localizacao = this.toObject(data, LocalizacaoDTO.class);
            this.service.update(localizacao.getLongitude(), localizacao.getLatitude());
            return this.success();
        } catch (Exception e) {
            return this.error(e);
        }
    }
    
    @POST
    @Produces("application/json")
    @Consumes("application/json")
    @Path("getLocation")
    public String getLocation(String data) {
        try {
            UsuarioDTO usuario = this.toObject(data, UsuarioDTO.class);
            Localizacao localizacao = this.service.getLocation(this.converter.getUsuarioDto().convert(usuario));
            return this.result(this.converter.getLocalizacao().convert(localizacao));
        } catch (Exception e) {
            return this.error(e);
        }
    }
}
